<?php

namespace App\Models\Entity\Pm;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Price
 * @package App\Models\Entity\Pm
 * @version August 1, 2017, 4:33 pm UTC
 */
class Price extends Model
{
    use SoftDeletes;

    public $table = 'prices';

    protected $dates = ['deleted_at'];

    public $fillable = [
        'name',
        'title',
        'subtitle',
        'short_desc',
        'prices',
        'pmtag_id',
        'metering_period_id',
        'type',
        'expiry_date_id',
        'sdk_price_id',
        'startdate',
        'enddate',
        'listorder',
        'created_at',
        'updated_at',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'name'               => 'string',
        'title'              => 'string',
        'subtitle'           => 'string',
        'short_desc'         => 'string',
        'pmtag_id'           => 'integer',
        'metering_period_id' => 'integer',
        'type'               => 'tinyInteger',
        'expiry_date_id'     => 'integer',
        'listorder'          => 'integer',
        'sdk_price_id'       => 'string',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'name'               => 'required|max:100',
        'title'              => 'max:100',
        'short_desc'         => 'required|max:100',
        'prices'             => 'required|numeric|max:100',
        'pmtag_id'           => 'max:100',
        'metering_period_id' => 'required_with:pmtag_id|max:100',
        'startdate'          => 'required|date',
        'enddate'            => 'required|date',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     **/
    public function vassetis()
    {
        return $this->belongsToMany(\App\Models\Entity\Asset\Video\Vasseti::class, 'price_vassetis', 'price_id', 'vasseti_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function pmtag()
    {
        return $this->hasOne(\App\Models\Entity\Pm\Pmtag::class, 'id', 'pmtag_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function meteringPeriod()
    {
        return $this->hasOne(\App\Models\Entity\Pm\MeteringPeriod::class, 'id', 'metering_period_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function expiryDate()
    {
        return $this->hasOne(\App\Models\Entity\Pm\ExpiryDate::class, 'id', 'expiry_date_id');
    }
}
